Passed
Push — master ( 09f8ce...cd7a74 )
by Jesús
02:16
created

toast.ts ➔ showDisabledBoxToast   A

Complexity

Conditions 3

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
dl 0
loc 15
rs 9.8
c 0
b 0
f 0
1
import { currentTranslations } from '../../language';
2
3
let toastTimeout: NodeJS.Timeout | null = null;
4
let toastRemoveTimeout: NodeJS.Timeout | null = null;
5
let toastShowTimeout: NodeJS.Timeout | null = null;
6
let clickCount = 0;
7
let clickResetTimeout: NodeJS.Timeout | null = null;
8
9
export function showDisabledBoxToast(): void {
10
  clickCount++;
11
12
  if (clickResetTimeout) {
13
    clearTimeout(clickResetTimeout);
14
  }
15
  clickResetTimeout = setTimeout(() => {
16
    clickCount = 0;
17
  }, 2000);
18
19
  // Show toast only after 2+ clicks
20
  if (clickCount >= 2) {
21
    showToast(currentTranslations.disabledBoxMessage);
22
    clickCount = 0;
23
  }
24
}
25
26
function showToast(message: string): void {
27
  const existingToast = document.getElementById('toast-notification');
28
  if (existingToast) {
29
    existingToast.remove();
30
  }
31
32
  // Clear all existing timers
33
  if (toastTimeout) clearTimeout(toastTimeout);
34
  if (toastRemoveTimeout) clearTimeout(toastRemoveTimeout);
35
  if (toastShowTimeout) clearTimeout(toastShowTimeout);
36
37
  const toast = document.createElement('div');
38
  toast.id = 'toast-notification';
39
  toast.className = 'toast';
40
  toast.textContent = message;
41
  toast.setAttribute('role', 'alert');
42
  toast.setAttribute('aria-live', 'polite');
43
44
  document.body.appendChild(toast);
45
46
  // Use setTimeout instead of requestAnimationFrame for better testability
47
  toastShowTimeout = setTimeout(() => {
48
    toast.classList.add('show');
49
  }, 0);
50
51
  toastTimeout = setTimeout(() => {
52
    toast.classList.remove('show');
53
    toastRemoveTimeout = setTimeout(() => {
54
      toast.remove();
55
    }, 300); // Wait for fade out animation
56
  }, 3000);
57
}
58